package cn.itcast.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.dao.PermissionDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Permission> selectAllPermisson() {
        log.info("进入查询所有");
        return  this.permissionDao.selectAllPermisson();
    }

    /**
     * 根据条件分页查询信息
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery( QueryPageBean queryPageBean) {
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //条件查询
        Page<Permission> permissions =  this.permissionDao.selectByCondition(queryPageBean.getQueryString());

        log.info("permission:"+permissions.getTotal()+" .......... .."+ permissions.getResult());
        return  new PageResult(permissions.getTotal(),permissions.getResult());
    }

    /**
     * 添加权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }


    /**
     * 确认修改数据
     * @param permission
     */
    @Override
    public void handleEdit( Permission permission) {
        this.permissionDao.update(permission);
    }

    /**
     * 删除权限
     * @param id
     */
    @Override
    public void handleDeleteById(Integer id) {
        this.permissionDao.deleteById(id);
    }


    /**
     * 根据id查询
     * @param id
     */
    @Override
    public Permission selectById(Integer id) {
        Permission permission =  this.permissionDao.selectById(id);
        return permission;
    }



    @Override
    public List<Permission> findAllPermission(Integer[] menuIds) {
        ArrayList<Permission> permissionList = new ArrayList<>();
        for (Integer menuId : menuIds) {
            List<Permission> ids = this.permissionDao.findPermission(menuId);
            if (ObjectUtil.isNotEmpty(ids)){
                for (Permission id : ids) {
                    permissionList.add(id);
                }
            }
        }
        return permissionList;
    }


}
